//
//  APIBaseManager+Interceptor.m
//  BaoZhangWang
//
//  Created by yyy on 2016/12/13.
//  Copyright © 2016年 yyy. All rights reserved.
//

#import "APIBaseManager+Interceptor.h"
#import "URLResponse.h"
#import "APIManager.h"

@implementation APIBaseManager (Interceptor)

#pragma mark - 拦截器
/*
 拦截器的功能可以由子类实现，也可以由其它对象实现,两种做法可以共存
 当两种情况共存的时候
 然后它们的调用顺序是BaseManager会先调用子类的实现，再调用外部interceptor的实现
 */
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"

- (BOOL)base_shouldCallAPIWithParams:(id)params {
    APIBaseManager <APIManager> *manager = (id <APIManager>) self;
    if ([manager respondsToSelector:@selector(shouldCallAPIWithParams:)]) {
        if (![manager shouldCallAPIWithParams:params]) {
            return NO;
        }
    }
    if (self.interceptor && [self.interceptor respondsToSelector:@selector(manager:shouldCallAPIWithParams:)]) {
        if (![self.interceptor manager:manager shouldCallAPIWithParams:params]) {
            return NO;
        }
    }
    return YES;
}

- (void)base_afterCallAPIWithParams:(id)params {
    APIBaseManager <APIManager> *manager = (id <APIManager>) self;
    if ([manager respondsToSelector:@selector(afterCallAPIWithParams:)]) {
        [manager afterCallAPIWithParams:params];
    }
    if (self.interceptor && [self.interceptor respondsToSelector:@selector(manager:afterCallAPIWithParams:)]) {
        [self.interceptor manager:manager afterCallAPIWithParams:params];
    }
}

- (id)base_APIReformParams:(id)params {
    APIBaseManager <APIManager> *manager = (id <APIManager>) self;
    if ([manager respondsToSelector:@selector(APIReformParams:)]) {
        params = [manager APIReformParams:params];
    }
    return params;
}

- (BOOL)base_beforePerformCallBackWithReponse:(URLResponse *)response {
    APIBaseManager <APIManager> *manager = (id <APIManager>) self;
    if ([manager respondsToSelector:@selector(beforePerformCallBackWithResponse:)]) {
        if (![manager beforePerformCallBackWithResponse:response]) {
            return NO;
        }
    }
    
    if (self.interceptor != nil && [self.interceptor respondsToSelector:@selector(manager:beforePerformCallBackWithResponse:)]) {
        if (![self.interceptor manager:self beforePerformCallBackWithResponse:response]) {
            return NO;
        }
    }
    
    return YES;
}

- (void)base_afterPerformCallBackWithResponse:(URLResponse *)response {
    APIBaseManager <APIManager> *manager = (id <APIManager>) self;
    if ([manager respondsToSelector:@selector(afterPerformCallBackWithResponse:)]) {
        [manager afterPerformCallBackWithResponse:response];
    }
    
    if (self.interceptor != nil && [self.interceptor respondsToSelector:@selector(manager:afterPerformCallBackWithResponse:)]) {
        [self.interceptor manager:self afterPerformCallBackWithResponse:response];
    }
}

- (BOOL)base_beforePerformSuccessWithResponse:(URLResponse *)response {
    APIBaseManager <APIManager> *manager = (id <APIManager>) self;
    if ([manager respondsToSelector:@selector(beforePerformSuccessWithResponse:)]) {
        if (![manager beforePerformSuccessWithResponse:response]) {
            return NO;
        }
    }
    
    if (self.interceptor != nil && [self.interceptor respondsToSelector:@selector(manager:beforePerformSuccessWithResponse:)]) {
        if (![self.interceptor manager:self beforePerformSuccessWithResponse:response]) {
            return NO;
        }
    }
    return YES;
}

- (void)base_afterPerformSuccessWithResponse:(URLResponse *)response {
    APIBaseManager <APIManager> *manager = (id <APIManager>) self;
    if ([manager respondsToSelector:@selector(afterPerformSuccessWithResponse:)]) {
        [manager afterPerformSuccessWithResponse:response];
    }
    
    if (self.interceptor != nil && [self.interceptor respondsToSelector:@selector(manager:afterPerformSuccessWithResponse:)]) {
        [self.interceptor manager:self afterPerformSuccessWithResponse:response];
    }
}

- (BOOL)base_beforePerformFailWithResponse:(URLResponse *)response {
    APIBaseManager <APIManager> *manager = (id <APIManager>) self;
    if ([manager respondsToSelector:@selector(beforePerformFailWithResponse:)]) {
        if (![manager beforePerformFailWithResponse:response]) {
            return NO;
        }
    }
    
    if (self.interceptor != nil && [self.interceptor respondsToSelector:@selector(manager:beforePerformFailWithResponse:)]) {
        if (![self.interceptor manager:self beforePerformFailWithResponse:response]) {
            return NO;
        }
    }
    return YES;
}

- (void)base_afterPerformFailWithResponse:(URLResponse *)response {
    APIBaseManager <APIManager> *manager = (id <APIManager>) self;
    if ([manager respondsToSelector:@selector(afterPerformFailWithResponse:)]) {
        [manager afterPerformFailWithResponse:response];
    }
    
    if (self.interceptor != nil && [self.interceptor respondsToSelector:@selector(manager:afterPerformFailWithResponse:)]) {
        [self.interceptor manager:self afterPerformFailWithResponse:response];
    }
}

#pragma clang diagnostic pop
@end
